﻿$(document).ready(function () {
    var fieldTypeSelect = $("select[name=FieldType]");
    var maxLenField = $("#MaxLength");
    var isRequiredField = $("#IsRequired");

    //fieldTypeSelect.toggle();

    fieldTypeSelect.change(function () {
        fieldTypeChanged(fieldTypeSelect.val());
    });
    maxLenField.change(function () {
        maxLengthChanged(maxLenField.val());
    });
    isRequiredField.change(function () {
        requiredChanged(isRequiredField.attr("checked"));
    });

    requiredChanged(isRequiredField.attr("checked"));
    fieldTypeChanged(fieldTypeSelect.val());
});

function maxLengthChanged(maxLen) {
    if (maxLen == "") {
        $("#DefaultValue").removeAttr("maxlength");
        return;
    }
    var len = parseInt(maxLen);
    if (len != undefined && !isNaN(len)) {
        var val = $("#DefaultValue").val();
        $("#DefaultValue").val(val.substring(0, len));
        $("#DefaultValue").attr("maxlength", len);
    }
}

function requiredChanged(checked) {
    if (checked) {
        if ($("#DefaultValue").attr("data-val-required") == undefined) {
            $("#DefaultValue").attr("data-val-required", "This field is required.");
        }
    }
    else {
        $("#DefaultValue").removeAttr("data-val-required");
    }
    validationChanged();
}

function fieldTypeChanged(fieldType) {
    var defaultValueDiv = $("#defaultValueDiv");
    defaultValueDiv.html("");

    var defaultValueField = $("#DefaultValue");
    defaultValueField.val("");
    defaultValueField.removeClass("datepicker");
    defaultValueField.datepicker("destroy");
    defaultValueField.removeAttr("data-val-number");
    
    $("#MaxLength").val("");
    /* hide max length if not needed */
    var maxLengthDiv = $("#maxLengthDiv");
    if (fieldType != "String")
        maxLengthDiv.hide();
    else
        maxLengthDiv.show();

    maxLengthChanged("");

    switch (fieldType) {
        case "Boolean":
            defaultValueField.hide();
            defaultValueField.val("False");
            defaultValueDiv.append(
                $(document.createElement("input"))
                .attr({ type: "checkbox" })
                .change(function () {
                    defaultValueField.val($(this).attr("checked") ? "True" : "False");
                })
            );
            break;
        case "DateTime":
            defaultValueField.show();
            defaultValueField.addClass("datepicker");
            defaultValueField.datepicker({
                changeMonth: true,
                changeYear: true,
                dateFormat: 'yy-mm-dd'
            });
            break;
        case "Integer":
            defaultValueField.show();
            defaultValueField.attr("data-val-number", "The field must be a number.");
            break;
        case "Double":
            defaultValueField.show();
            defaultValueField.attr("data-val-number", "The field must be a number.");
            break;
        default:
            defaultValueField.show();
    }

    validationChanged();
}

function clearValidationInfo() {
    $("span[data-valmsg-for=DefaultValue]").hide();
    $("span[data-valmsg-for=MaxLength]").hide();
    $("#DefaultValue").css("background-color", "none");
    $("#MaxLength").css("background-color", "none");
}

function validationChanged() {
    $("form").removeData("validator");
    $("form").removeData("unobtrusiveValidation");
    $.validator.unobtrusive.parse("form");
}